package com.example.concurrent;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class AQS01_Synchronized implements BeanPostProcessor {
    //临界资源（即共享变量）
    private static final int i = 0;

    public static void main(String[] args) throws InterruptedException {
        /**
         * 线程1单独访问i，偏向锁
         */
        new Thread(() -> System.out.println("线程1：" + i)).start();

        /**
         * 线程1和2交替访问i，升级为轻量级锁
         */
        Thread.sleep(1000); //睡眠是为了保证线程1执行完再交替执行线程2
        new Thread(() -> System.out.println("线程2：" + i)).start();

        /**
         * 并发访问i，升级为重量级锁
         */
        for (int j = 3; j < 6; j++) {
            int finalJ = j;
            Thread thread = new Thread(() -> {
                System.out.println("线程" + finalJ + ":" + i);
            });
            thread.start(); //让3个创建好的线程同时启动
        }
    }

}
